# -*- coding: utf-8 -*-
from django.db import models
from django.contrib.auth.models import AbstractUser
from markdownx.models import MarkdownxField

# Create your models here.

# 用户模型
class User(AbstractUser):
    avatar = models.ImageField(upload_to='avator/%Y/%m', default='avatar/default.png',
                               max_length=200, blank=True, null=True, verbose_name='用户头像')
    mobile = models.CharField(max_length=11, blank=True, null=True, unique=True, verbose_name='手机号码')

    class Meta:
        verbose_name = '用户'
        verbose_name_plural = verbose_name
        ordering = ['-id']

    def __str__(self):
        return self.username

# 访问统计
class VisitCount(models.Model):
    date_count = models.DateField(verbose_name='统计日期')
    page_view = models.IntegerField(default=0, verbose_name='页面访问量(PV)')
    unique_vistor = models.IntegerField(default=0, verbose_name='访问用户量(UV)')

    class Meta:
        verbose_name = '访问统计'
        verbose_name_plural = verbose_name

    def __str__(self):
        return str(self.date_count)

# Images（图片管理）
class Images(models.Model):
    name = models.ImageField(upload_to='imgs/', default='imgs/default.png',
                             max_length=200, blank=True, null=True, verbose_name='图像资源')

    class Meta:
        verbose_name = '图片管理'
        verbose_name_plural = verbose_name

    def __str__(self):
        return str(self.name)


class BaseInfo(models.Model):
    title = models.CharField(max_length=255, verbose_name="站点标题")
    rss_url = models.CharField(max_length=255, verbose_name="RSS订阅")
    github_url = models.CharField(max_length=255, verbose_name="Git链接", null=True, blank=True)
    weibo_url = models.CharField(max_length=255, verbose_name="微博链接")
    wechat_img = models.ImageField(upload_to='wechat/', default='wechat/default.png',
                                   max_length=200, blank=True, null=True, verbose_name='微信二维码')

    class Meta:
        verbose_name = '站点信息'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.title


# 分类
class Category(models.Model):
    name = models.CharField(max_length=30, verbose_name='分类名称')
    desc = models.CharField(max_length=255, default=None, verbose_name='分类描述')
    index = models.IntegerField(default=999, verbose_name='分类的排序')

    class Meta:
        verbose_name = '分类'
        verbose_name_plural = verbose_name
        ordering = ['index', 'id']

    def __str__(self):
        return self.name

    def save(self, *args, **kwargs):
        # if self.name == 'xx':
        #     return
        # else:
        # self.name = 'help'
        super(Category, self).save(*args, **kwargs)

# tag（标签）
class Tag(models.Model):
    name = models.CharField(max_length=30, verbose_name='标签名称')

    class Meta:
        verbose_name = '标签'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name


# 来源
class Source(models.Model):
    name = models.CharField(max_length=30, verbose_name='来源名称')

    class Meta:
        verbose_name = '文章来源'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name


# 自定义一个文章Model的管理器
# 1、新加一个数据处理的方法
# 2、改变原有的queryset
class ArticleManager(models.Manager):
    def distinct_date(self):
        distinct_date_list = []
        date_list = self.values('date_publish')
        for date in date_list:
            date = date['date_publish'].strftime('%Y/%m/%d')
            if date not in distinct_date_list:
                distinct_date_list.append(date)
        return distinct_date_list


# 文章模型
class Article(models.Model):
    objects = ArticleManager()
    title = models.CharField(max_length=50, verbose_name='文章标题')
    desc = models.CharField(max_length=50, verbose_name='文章描述')
    # content = models.TextField(verbose_name='文章内容')
    content = MarkdownxField(verbose_name='文章内容')
    click_count = models.IntegerField(default=0, verbose_name='点击次数')
    is_recommend = models.BooleanField(default=False, verbose_name='是否推荐')
    date_publish = models.DateTimeField(auto_now_add=True, verbose_name='发布时间')
    user = models.ForeignKey(User, verbose_name='用户')
    category = models.ForeignKey(Category, related_name="related_article_category", blank=True, null=True, verbose_name='分类')
    tag = models.ManyToManyField(Tag, related_name="related_article_tag", verbose_name='标签')


    class Meta:
        verbose_name = '文章'
        verbose_name_plural = verbose_name
        ordering = ['-date_publish']

    def __str__(self):
        return self.title


# 评论模型
class Comment(models.Model):
    content = models.TextField(verbose_name='评论内容')
    username = models.CharField(max_length=30, blank=True, null=True, verbose_name='用户名')
    email = models.EmailField(max_length=50, blank=True, null=True, verbose_name='邮箱地址')
    url = models.URLField(max_length=100, blank=True, null=True, verbose_name='个人网页地址')
    date_publish = models.DateTimeField(auto_now_add=True, verbose_name='发布时间')
    user = models.ForeignKey(User, blank=True, null=True, verbose_name='用户')
    article = models.ForeignKey(Article, blank=True, null=True, verbose_name='文章', related_name="related_article")
    pid = models.ForeignKey('self', blank=True, null=True, verbose_name='父级评论')

    class Meta:
        verbose_name = '评论'
        verbose_name_plural = verbose_name

    def __str__(self):
        return str(self.id)


# 友情链接
class Links(models.Model):
    title = models.CharField(max_length=50, verbose_name='标题')
    description = models.CharField(max_length=200, verbose_name='友情链接描述')
    callback_url = models.URLField(verbose_name='url地址')
    date_publish = models.DateTimeField(auto_now_add=True, verbose_name='发布时间')
    index = models.IntegerField(default=999, verbose_name='排列顺序(从小到大)')

    class Meta:
        verbose_name = '友情链接'
        verbose_name_plural = verbose_name
        ordering = ['index', 'id']

    def __str__(self):
        return self.title


class Guid(models.Model):
    name = models.CharField(max_length=50, verbose_name='名称')
    url = models.CharField(max_length=50, verbose_name='链接')
    order = models.IntegerField(verbose_name='顺序')

    class Meta:
        verbose_name = u'导航信息'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name


class Plugins(models.Model):
    title = models.CharField(max_length=50, verbose_name='插件标题')
    note = models.CharField(max_length=50, verbose_name='备注')
    content = models.TextField(verbose_name='正文')
    order = models.IntegerField(verbose_name='顺序')
    disabled = models.BooleanField(verbose_name='是否禁用')

    class Meta:
        verbose_name = '插件信息'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.title
